package com.example.oj.service.impl;

import java.time.LocalDateTime;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.oj.entity.Question;
import com.example.oj.entity.SysOption;
import com.example.oj.entity.User;
import com.example.oj.entity.vo.UserVO;
import com.example.oj.mapper.QuestionMapper;
import com.example.oj.mapper.SysOptionMapper;
import com.example.oj.mapper.UserMapper;
import com.example.oj.service.QuestionAnswerService;
import com.example.oj.service.QuestionService;
import com.example.oj.service.SysOptionService;
import com.example.oj.service.UserService;
import com.example.ojcommon.common.JudgeCase;
import com.example.ojcommon.common.JudgeConfig;
import com.example.ojcommon.constant.CommonConstant;
import com.example.oj.entity.dto.question.QuestionQueryRequest;
import com.example.oj.entity.vo.QuestionVO;
import com.example.ojcommon.enums.DifficultyEnum;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lenovo131
 * @description 针对表【question(题目)】的数据库操作Service实现
 * @createDate 2023-10-25 21:42:23
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question>
    implements QuestionService {

  @Resource
  private QuestionMapper questionMapper;

  @Resource
  private UserService userService;

  @Resource
  private SysOptionService optionService;

  @Resource
  private QuestionAnswerService questionAnswerService;

  @Override
  public QuestionVO getQuestionVO(Question question) {
    if (question == null) {
      return null;
    }
    QuestionVO questionVO = new QuestionVO();
    questionVO.setId(question.getId());
    questionVO.setCode(question.getCode());
    questionVO.setCompetitionId(question.getCompetitionId());
    questionVO.setTitle(question.getTitle());
    questionVO.setContent(question.getContent());
    questionVO.setScore(question.getScore());
    List<SysOption> tagsList = optionService.getOptionsListByType("题目标签");
    List<String> tagsText = new ArrayList<>();
    List<String> list = Arrays.asList(question.getTags().split(","));
    List<Long> tags = list.stream().map(Long::parseLong).collect(Collectors.toList());
    questionVO.setTags(tags);
    for (String s : list) {
      for (SysOption obj : tagsList) {
        if (obj.getId().toString().equals(s)) {
          tagsText.add(obj.getText());
          break;
        }
      }
    }
    questionVO.setTagsText(tagsText);
    questionVO.setDifficulty(question.getDifficulty());
    questionVO.setDifficultyText(DifficultyEnum.valueOf(question.getDifficulty()));
    questionVO.setSubmitNum(question.getSubmitNum());
    questionVO.setAcceptedNum(question.getAcceptedNum());
    questionVO.setThumbNum(question.getThumbNum());
    questionVO.setJudgeConfig(JSONUtil.toBean(question.getJudgeConfig(), JudgeConfig.class));
//        questionVO.setInputRefer(question.getInputRefer());
//        questionVO.setOutputRefer(question.getOutputRefer());
    questionVO.setJudgeCaseList(JSONUtil.toList(question.getJudgeCase(), JudgeCase.class));
    questionVO.setCreateTime(question.getCreateTime().toLocalDate());
    User user = userService.getById(question.getUserId());
    UserVO userVO = userService.getUserVO(user);
    questionVO.setUser(userVO);
    Integer hasAnswer = questionAnswerService.getHasAnswer(question.getId());
    questionVO.setHasAnswer(hasAnswer);
    questionVO.setIsQueried(question.getIsQueried());
    return questionVO;
  }

  @Override
  public QuestionVO getQuestionVOById(Long questionId) {
    if (questionId == null) {
      return null;
    }
    Question question = questionMapper.selectById(questionId);
    return getQuestionVO(question);
  }

  @Override
  public Page<QuestionVO> getQuestionVOPage(Page<Question> questionPage) {
    List<Question> questionList = questionPage.getRecords();
    Page<QuestionVO> questionVOPage = new Page<>(questionPage.getCurrent(), questionPage.getSize(), questionPage.getTotal());
    if (CollectionUtils.isEmpty(questionList)) {
      return questionVOPage;
    }
    List<QuestionVO> collect = questionList.stream().map(this::getQuestionVO).collect(Collectors.toList());
    questionVOPage.setRecords(collect);
    return questionVOPage;
  }

  @Override
  public List<Question> getQuestionPage(QuestionQueryRequest questionQueryRequest) {
    Long code = questionQueryRequest.getCode();
    String title = questionQueryRequest.getTitle();
    List<String> tags = questionQueryRequest.getTags();
    Integer difficulty = questionQueryRequest.getDifficulty();
    String startTime = questionQueryRequest.getStartTime();
    String endTime = questionQueryRequest.getEndTime();
    Long userId = questionQueryRequest.getUserId();
    List<Question> questionList = questionMapper.getQuestionPage(code, title, tags, difficulty, startTime, endTime,userId);

    return questionList;
  }

  @Override
  public void updateQuestionNum(Long questionId) {
    questionMapper.updateQuestionNum(questionId);
  }


  @Override
  public Page<QuestionVO> listPageCompetitionQuestionVO(Long competitionId) {
    List<Question> list = questionMapper.listPageCompetitionQuestion(competitionId);
    List<QuestionVO> questionVOList = list.stream().map(this::getQuestionVO).collect(Collectors.toList());
    Page<QuestionVO> questionVOPage = new Page<>(0,10);
    questionVOPage.setRecords(questionVOList);
    return questionVOPage;
  }

  @Override
  public void updateCompetitionId(List<Long> questionList, Long competitionId) {
    StringBuilder sb = new StringBuilder();
    for (Long id : questionList) {
      sb.append(id).append(",");
    }
    questionMapper.updateCompetitionId(sb.toString(),competitionId);
  }

  @Override
  public void removeCompetitionId(Long id) {
    questionMapper.removeCompetitionId(id);
  }

  @Override
  public void updateQuestionIsQueried(Long id) {
    questionMapper.updateQuestionIsQueried(id);
  }
}




